package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Conference;
import com.ruoyi.system.service.IConferenceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会议Controller
 *
 * @author ruoyi
 * @date 2023-12-16
 *
 * 接口
 * 获取会议列表
 * 获取会议详细信息
 * 新增会议信息
 * 修改会议信息
 * 删除会议信息
 * 开始会议
 * 结束会议
 * 校验密码（最多输入三次）
 */
@Controller
@RequestMapping("/system/conference")
public class ConferenceController extends BaseController
{
    private String prefix = "system/conference";

    @Autowired
    private IConferenceService conferenceService;

    @RequiresPermissions("system:conference:view")
    @GetMapping()
    public String conference()
    {
        return prefix + "/conference";
    }

    /**
     * 跳转到会议文件页面
     * @param id
     * @param modelMap
     * @return
     **/
    @GetMapping("/showPapers/{id}")
    public String conference(@PathVariable Integer id, ModelMap modelMap) {
        modelMap.put("conId", id);
        return "system/papers/papers";
    }

    /**
     * 查询会议列表
     */
    @RequiresPermissions("system:conference:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Conference conference)
    {
        startPage();
        List<Conference> list = conferenceService.selectConferenceList(conference);
        return getDataTable(list);
    }

//    /**
//     * 导出会议列表
//     */
//    @RequiresPermissions("system:conference:export")
//    @Log(title = "会议", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(Conference conference)
//    {
//        List<Conference> list = conferenceService.selectConferenceList(conference);
//        ExcelUtil<Conference> util = new ExcelUtil<Conference>(Conference.class);
//        return util.exportExcel(list, "会议数据");
//    }

    /**
     * 新增会议
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存会议
     */
    @RequiresPermissions("system:conference:add")
    @Log(title = "会议", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Conference conference)
    {
        // 获取当前登录人信息  存储
        conference.setCreatedBy(getUserId().toString());
        conference.setCreatedTime(new Date());
        return toAjax(conferenceService.insertConference(conference));
    }

    /**
     * 修改会议
     */
    @RequiresPermissions("system:conference:edit")
    @GetMapping("/edit/{conferenceId}")
    public String edit(@PathVariable("conferenceId") Long conferenceId, ModelMap mmap)
    {
        Conference conference = conferenceService.selectConferenceByConferenceId(conferenceId);
        mmap.put("conference", conference);
        return prefix + "/edit";
    }

    /**
     * 修改保存会议
     */
    @RequiresPermissions("system:conference:edit")
    @Log(title = "会议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Conference conference)
    {
        return toAjax(conferenceService.updateConference(conference));
    }

    /**
     * 删除会议
     */
    @RequiresPermissions("system:conference:remove")
    @Log(title = "会议", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(conferenceService.deleteConferenceByConferenceIds(ids));
    }

    /**
     * 开始会议的接口
     */
    @ResponseBody
    @GetMapping("/startMeeting/{conferenceId}")
    public AjaxResult startMeeting(@PathVariable("conferenceId") Long conferenceId)
    {
        Conference conference=new Conference();
        conference.setConferenceId(conferenceId);
        conference.setBeginTime(new Date());
//        conference.setEndTime(null);
        conference.setConferenceState("1");

        return toAjax(conferenceService.updateConference(conference));
    }

    /**
     * 结束会议的接口
     */
    @ResponseBody
    @GetMapping("/endMeeting/{conferenceId}")
    public AjaxResult endMeeting(@PathVariable("conferenceId") Long conferenceId)
    {
        // 判断会议是否结束
        Conference conferenceOld=conferenceService.selectConferenceByConferenceId(conferenceId);
        if (Objects.equals(conferenceOld.getConferenceState(), "0")) {
            return error("会议未开始，不能结束");
        }
        Conference conference=new Conference();
        conference.setConferenceId(conferenceId);
        // if()
        conference.setEndTime(new Date());
        conference.setConferenceState("2");

        return toAjax(conferenceService.updateConference(conference));
    }
}
